#!/usr/bin/perl
use strict;

my %chg = ("A", "ADE", "U", "URI", "C", "CYT", "G", "GUA",);

open(IN, "< $ARGV[0]") or die;
open(OUT, "> $ARGV[0]\-edit.pdb") or die;
my $count = 0;
my $change = 0;
while(defined(my $line = <IN>)) {
  if($line =~ /^ATOM/ || $line =~ /^HETATM/) {
    my $res = substr($line,17,3);
    $res =~ s/^\s+//; $res =~ s/\s+$//;
    if($chg{$res}) {
      substr($line,17,3) = $chg{$res};
      $change++;
    }
    $line =~ s/\*/\'/g;
    print OUT $line;
    $count++;
    if($count % 3000 == 0) { print STDERR "."; }
  }
}
print STDERR "\nchanged $change of $count\n\n";
